package org.ehe.erp.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.monitor.MonitorMethod;
import org.ehe.business.overdue.extract.FeedBackDataExtractor;
import org.ehe.business.timeout.contants.BizTypeConstant;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.quality.bo.ErpQualityRecordBo;
import org.ehe.erp.domain.quality.dto.QualityRecordQueryDto;
import org.ehe.erp.domain.quality.vo.ErpQualityRecordVo;
import org.ehe.erp.service.quality.ErpQualityRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质检记录详情控制器
 * @author zhangnn
 * @date 2025-09-11
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/quality/record")
public class ErpQualityRecordController extends BaseController {

    private final ErpQualityRecordService recordService;

    /**
     * 分页列表
     */
    @SaCheckPermission("quality:record:list")
    @GetMapping("/list")
    public TableDataInfo<ErpQualityRecordVo> list(QualityRecordQueryDto dto, PageQuery pageQuery) {
        return recordService.queryPageList(dto, pageQuery);
    }

    /**
     * 不分页列表
     */
    @SaCheckPermission("quality:record:list")
    @GetMapping("/listAll")
    public R<List<ErpQualityRecordVo>> listAll(QualityRecordQueryDto dto) {
        return R.ok(recordService.queryList(dto));
    }

    /**
     * 详情
     */
    @SaCheckPermission("quality:record:query")
    @GetMapping("/{id}")
    public R<ErpQualityRecordVo> getInfo(@PathVariable Long id) {
        return R.ok(recordService.queryById(id));
    }

    /**
     * 新增
     */
    //@MonitorMethod(bizType = BizTypeConstant.FEEDBACK+"Overdue", extractor = FeedBackDataExtractor.class)
    @SaCheckPermission("quality:record:add")
    @Log(title = "新增质检记录", linkType = "quality", businessType = BusinessType.INSERT, idField = "id", nameField = "itemName")
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpQualityRecordBo bo) {
        return toAjax(recordService.insertByBo(bo));
    }

    /**
     * 修改
     */
    @SaCheckPermission("quality:record:edit")
    @Log(title = "修改质检记录", linkType = "quality", businessType = BusinessType.UPDATE, idField = "id", nameField = "itemName")
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpQualityRecordBo bo) {
        return toAjax(recordService.updateByBo(bo));
    }

    /**
     * 删除
     */
    @SaCheckPermission("quality:record:remove")
    @Log(title = "删除质检记录", linkType = "quality", businessType = BusinessType.DELETE, idField = "id", nameField = "itemName")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(recordService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据质检任务ID查询记录
     */
    @SaCheckPermission("quality:record:query")
    @GetMapping("/getByTaskId/{taskId}")
    public R<List<ErpQualityRecordVo>> getByTaskId(@PathVariable Long taskId) {
        return R.ok(recordService.getByTaskId(taskId));
    }

    /**
     * 批量保存（前端一次性提交多条）
     */
    @SaCheckPermission("quality:record:add")
    @Log(title = "质检记录", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public R<Void> batchSave(@RequestBody List<ErpQualityRecordBo> bos) {
        return toAjax(recordService.batchInsert(bos));
    }
}
